﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using WebUI.Helper;


namespace WebUI.Con
{
    public class CheckoutController:Controller
    {
        
        public static string NickNime { get; set; }
        public static decimal CostMonery { get; set; }
        public static decimal Total { get; set; }
        public static decimal TakeMonery { get; set; }
       
        public ActionResult index()
        {
            Model.Checkout PreCheckout = OperateContext.Current.BLLSession.ICheckoutBLL.GetListBy(s => s.CheckoutID > 0, s => s.CheckoutID, false).ToList().FirstOrDefault();
            DateTime t = PreCheckout.CheckoutTime??DateTime.Now;
            NickNime = OperateContext.Current.Usr.UserName;
            TempData["NickNime"] = NickNime;
            Total= OperateContext.Current.BLLSession.ISaleTDBLL.GetListBy(s => (s.SaleTime ?? DateTime.Now).CompareTo(t) > 0&&(s.IsExchange??false)==false).Select(s => (s.Good.SalePrice ?? 0) * (s.SaleNumber ?? 0)).Sum(s => s);
            TempData["TotalSaleMoney"] = Total.ToString("0.00");
            CostMonery = OperateContext.Current.BLLSession.ICostBLL.GetListBy(s => (s.CostTime ?? DateTime.Now).CompareTo(t) > 0).Sum(s => s.CostAmount??0);
            TempData["CostMonery"] = CostMonery.ToString("0.00");
            TakeMonery = OperateContext.Current.BLLSession.ITakeMoneyBLL.GetListBy(s => (s.TakeMoneyTime ?? DateTime.Now).CompareTo(t) > 0).Sum(s => s.TakeMoneySum ?? 0);
            TempData["TakeMonery"] = TakeMonery.ToString("0.00");
            TempData["PreDrawerSum"] = (PreCheckout.DrawerSum ?? 0).ToString("0.00");
            return View();
        }
        public ActionResult Checkout()
        {
            if (Request["CheckOutMoney"] != null && Request["CheckOutMoney"].ToString() != "")
            {
                Model.Checkout ck = new Model.Checkout();
                ck.CheckoutTime = DateTime.Now;
                ck.CheckoutUserID = OperateContext.Current.Usr.UserID;
                ck.DrawerSum = Convert.ToDecimal(Request["CheckOutMoney"]);
                ck.CheckoutSaleAmount = Total;
                ck.CheckTakeMonery = TakeMonery;
                ck.CheckoutCostAmount = CostMonery;
                ck.ShiftID = OperateContext.Current.Shift.ShiftID;
                ck.CheckoutError = ck.DrawerSum - (OperateContext.Current.BLLSession.ICheckoutBLL.GetListBy(s => s.CheckoutID > 0, s => s.CheckoutID, false).ToList().FirstOrDefault().DrawerSum + Total - CostMonery - TakeMonery);
                int p = OperateContext.Current.BLLSession.ICheckoutBLL.Add(ck);
                if (p == 1)
                {
                    return Content("ok");
                }
                else { return Content("Error"); }
            }
           

            return Content("Error");
        }
       
    }
}
